import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/login/model/models.dart';
import 'package:nft_shop/main.dart';
import 'package:nft_shop/my/page/set/my_aboutus_page.dart';
import 'package:nft_shop/my/page/set/my_agreement_page.dart';
import 'package:nft_shop/my/page/set/my_contactus_page.dart';
import 'package:nft_shop/my/page/set/help/page/my_help_center.dart';
import 'package:nft_shop/my/page/set/my_safety_page.dart';
import 'package:nft_shop/my/page/set/payment_account_page.dart';
import 'package:nft_shop/my/page/write/my_write_page.dart';
import 'package:nft_shop/my/view/album_hint.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/other_utils.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:package_info/package_info.dart';

class MySetPage extends StatefulWidget {
  const MySetPage({Key? key}) : super(key: key);

  @override
  _MySetPageState createState() => _MySetPageState();
}

class _MySetPageState extends State<MySetPage> {
  String version = "";
  String buildNumber = "";

  updateVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      version = packageInfo.version; //版本号
      buildNumber = packageInfo.buildNumber; //版本构建号
    });
  }

  @override
  void initState() {
    super.initState();
    updateVersion();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "设置",
      ),
      body: MyScrollView(
        children: [
          setItemWidget("收款账号", onPressed: () => Get.to(PaymentAccountPage())),
          setItemWidget("安全中心", onPressed: () => Get.to(MySafetyPage())),
          setItemWidget("联系我们", onPressed: () => Get.to(MyContactUsPage())),
          setItemWidget("关于我们", onPressed: () => Get.to(MyAboutUsPage())),
          setItemWidget("协议", onPressed: () => Get.to(MyAgreementPage())),
          setItemWidget("帮助中心", onPressed: () => Get.to(MyHelpCenter())),
          setItemWidget("创作服务", onPressed: () {
            if (UserController.to.member.castRole == "0") {
              EasyLoading.showToast("请联系客服开通创作者服务权限");
              return;
            } else if (UserController.to.member.realNameStatus != "2") {
              if (UserController.to.member.realNameStatus == "1") {
                EasyLoading.showToast("存在待审核申请,请等待审核结果");
                return;
              }
              Utils.showElasticDialog<void>(
                context: context,
                barrierDismissible: false,
                builder: (BuildContext context) {
                  return AuthenticationDialog();
                },
              );
              return;
            }
            Get.to(() => MyWritePage());
          }),
          setItemWidget("当前版本",
              describe: Global.isAndroid ? version : buildNumber),
        ],
        bottomButton: Container(
          height: 40,
          margin: EdgeInsets.only(
              left: 14,
              right: 14,
              bottom: 27 + ScreenUtil.getBottomBarH(context)),
          decoration: BoxDecoration(
            color: Global.kTheme.colorBackground3,
            borderRadius: BorderRadius.circular(4),
          ),
          child: MyButton(
            minHeight: 40,
            onPressed: () {
              UserController.to.clearUser();
              eventBus.fire("logout");
              Get.back();
            },
            text: "退出登录",
            fontSize: 16,
            textColor: Colors.white,
            backgroundColor: Global.kTheme.colorBackground3,
          ),
        ),
      ),
    );
  }

  //itemCell
  Widget setItemWidget(String title,
      {GestureTapCallback? onPressed, String? describe}) {
    return InkWell(
      onTap: onPressed,
      child: Container(
        margin: EdgeInsets.only(left: 16),
        padding: EdgeInsets.only(top: 20, bottom: 20, right: 16),
        decoration: BoxDecoration(
            border: Border(
          bottom: Divider.createBorderSide(context,
              color: Global.kTheme.colorLine, width: 1),
        )),
        child: Row(
          children: [
            Text(
              title,
              style: TextStyle(
                  fontSize: 14,
                  color: Global.kTheme.textPrimary6,
                  fontFamily: Global.myRegularFont),
            ),
            Spacer(),
            describe == null
                ? LoadAssetImage(
                    "next_icon",
                    width: 7,
                    height: 12,
                  )
                : Text(
                    describe,
                    style: TextStyle(
                        fontSize: 14,
                        color: Global.kTheme.textPrimary6,
                        fontFamily: Global.mySemiboldFont),
                  ),
          ],
        ),
      ),
    );
  }
}
